<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsBrand;
use App\Store\Models\Basic\Supply;

class GoodsStockin extends Model
{
    protected $table = 'site_goods_stockin';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        ini_set('memory_limit', -1);  // 或更高的值
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo', 'goodsinfolist','brandinfo','supply','goodsinfolist.goods_category','goodsinfolist.supply','goodsinfolist.goods_style','goodsinfolist.goods_craft','goodsinfolist.goods_pricetype','goodsinfolist.goods_stone','goodsinfolist.paytype_info','goodsinfolist.goods_brand'])
            ->where(['cp_id' => $cp_id])
            ->orderBy('create_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if ($store_ids) { //权限控制
            if (is_array($store_ids)) { //获取当前权限下的门店
                $query->whereIn('store_id', $store_ids);
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) { //获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $store_id = intval($request->store_id); //下拉单选搜索
        if ($store_id > 0) {
            $query->where('store_id', $store_id);
        }

        $supply_id = intval($request->supply_id); //下拉单选搜索
        if ($supply_id > 0) {
            $query->where('supply_id', $supply_id);
        }

        $audit_status = intval($request->audit_status); //下拉单选搜索
        if ($audit_status > 0) {
            $query->where('audit_status', $audit_status);
        }else{
            $not_audit=intval($request->not_audit);

            if($not_audit>=1){
                $query->whereIn('audit_status',[1,2]);
            }
        }

        $order_sncode=$request->order_sncode;
        if($order_sncode!=''){
            $query->where('order_sncode','like','%'.$order_sncode.'%');
        }

        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        $paymoney=intval($request->paymoney);
        if($paymoney>0){
            $query->where('paymoney',$paymoney);
        }

        $isgetall = intval($request->isgetall) ?? 0;


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id','store_name');
    }

    public function goodsinfolist()
    {
        return $this->hasMany(GoodsStockinInfolist::class, 'p_id', 'id');
    }

    public function supply(){
        return $this->hasOne(Supply::class, 'id', 'supply_id')->select('id','supply_name');
    }

    public function brandinfo(){
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id','brand_name');
    }

    /**
     *获取列表
     */
    public static function electronicgetlist(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['goodsinfolist','brandinfo','supply','goodsinfolist.goods_category','goodsinfolist.supply','goodsinfolist.goods_style','goodsinfolist.goods_craft','goodsinfolist.goods_pricetype','goodsinfolist.goods_stone','goodsinfolist.paytype_info','goodsinfolist.goods_brand'])
            ->where(['cp_id' => $cp_id])
            ->orderBy('create_at', 'asc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if ($store_ids) { //权限控制
            if (is_array($store_ids)) { //获取当前权限下的门店
                $query->whereIn('store_id', $store_ids);
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) { //获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        //没有打印的电子秤入库数据
        $query->where('electronic_scale', 1);

        $isgetall = intval($request->isgetall) ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

}
